package com.cloud.controller;

import com.cloud.service.FeignService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import javafx.beans.DefaultProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * feign控制层
 *
 * @author : z_sz
 * @date : 2020-08-03 16:17
 **/
@RestController
@RequestMapping("/feignTest")
@DefaultProperties(defaultFallback = "payGlobalFallBackMethod")//默认降级方法
public class FeignController {

    @Autowired
    FeignService feignService;

    /**
     * 购买商品
     * @param goodsName 商品名
     * @param price 商品价格
     * @return 购买结果
     */
    @GetMapping("/payGoods")
    @HystrixCommand(fallbackMethod = "payGoodsFallBackMethod",
    // 设置超时时间，接口访问超过1500毫秒即启动服务降级，切换兜底的方法：payGoodsFallBackMethod
    commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500")})
    public String payGoods(@RequestParam("goodsName") String goodsName, @RequestParam("price") Double price) {
        return feignService.payGoods(goodsName,price);
    }

    /**
     * 购买商品方法的降级方法
     * @param goodsName
     * @param price
     * @return
     */
    @HystrixCommand
    public String payGoodsFallBackMethod(@RequestParam("goodsName") String goodsName, @RequestParam("price") Double price) {
        return "支付业务正忙，请稍后再试";
    }

    /**
     * 全局兜底方法
     * @return
     */
    public String payGlobalFallBackMethod() {
        return "当前服务器正忙，请稍后再试";
    }

}
